<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/8
 * Time: 16:13
 */
require '../DbTools.php';

//判断登录是否成功
$username = $_POST['username'];
$psd = $_POST['psd'];
$newUrl = '';
$msg = '';

//开启session
session_start();
//连接数据库
//$conn = new mysqli();
//$conn->connect('127.0.0.1','root','root','newBank');
////检测连接是否成功
//if(!empty($conn->connect_error)){
//    echo '数据库连接错误'.$conn->connect_error;
//    die;
//}
//echo '连接成功';
//调用查的方法
//DbTools::setConn($conn);
//DbTools::select('select * from bankcard ');
//print_r(DbTools::select($sql));
//S 修改
DbTools::$conn=new mysqli("127.0.0.1","root","root","newBank");

$sql="select * from bankcard WHERE CardNo='".$username."' and CardPwd='".$psd."'";
//count(DbTools::select($sql));只要卡号存在且密码正确就能查到 如果结果为0则认为密码错误
$result=count(DbTools::select($sql));

if($result>0){
//E 修改
    //echo '登录成功！';
    //设置cookie
    setcookie('userkey',md5($username.$psd),time()+24*3600);
    //将用户名与密码装进session里
    $_SESSION[md5($username.$psd)]=[
        'username'=>$username,
        'psd'=>$psd
    ];
    //登录成功后直接跳转到用户中心(userinfo)页面
    //1.方法一 直接跳转，无需其它步骤
    //header('location:userinfo.php');
    //2.方法二
    $newUrl = 'userinfo.php';
    $msg = '登录成功！';
}else{
    //echo '用户名或密码错误';
    //登录失败后直接跳转回到登录页面
    // /login.php =》是因为在不同目录里调用,所以需要用到 /根目录
    //1.方法一
    //header('location:/login.php');
    //2.方法二
    $newUrl = '/lession/login.php';
    $msg = '用户名或密码错误';
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <!-- hidden 隐藏-->
    <input type="hidden" id="newUrl" value="<?php echo $newUrl;?>">
    <input type="hidden" id="msg" value="<?php echo $msg;?>">
</body>
<script>
    onload = function () {
        var newUrl=document.getElementById('newUrl').value;
        var msg=document.getElementById('msg').value;
        //弹框 弹出判断结果
        alert(msg);
        //跳转到相应页面
        location.href = newUrl;
    }
</script>
</html>

